package com.example.express.controller;

import com.example.express.bean.BootStrapTableExpress;
import com.example.express.bean.Express;
import com.example.express.bean.Message;
import com.example.express.bean.ResultData;
import com.example.express.mvc.ResponseBody;
import com.example.express.service.BaseExpressService;
import com.example.express.service.impl.ExpressServiceImpl;
import com.example.express.util.DateFormatUtil;
import com.example.express.util.JSONUtil;
import com.example.express.util.UserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {

    private BaseExpressService expressService = new ExpressServiceImpl();
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String,Integer>> data = expressService.console();
        Message msg = new Message();
        if (data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req,HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNUm = Integer.parseInt(req.getParameter("pageNumber"));
        List<Express> data = expressService.findAll(true, offset, pageNUm);
        List<BootStrapTableExpress> list = new ArrayList<>();
        for (Express e:data){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTIme = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress express = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTIme,status,e.getSysPhone());
            list.add(express);
        }
        List<Map<String, Integer>> console = expressService.console();
        Integer total = console.get(0).get("data1_size");
        ResultData<BootStrapTableExpress> resultData = new ResultData<>();
        resultData.setRows(list);
        resultData.setTotal(total);
        String json = JSONUtil.toJSON(resultData);
        return json;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req,HttpServletResponse resp){
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        Express express = new Express(number,username,userPhone,company, UserUtils.getUserPhone(req.getSession()));
        boolean flag = expressService.insert(express);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("快递录入成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("快递录入失败");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req,HttpServletResponse resp){
        String number = req.getParameter("number");
        Express e = expressService.findByNumber(number);
        Message msg = new Message();
        if (e==null){
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req,HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        int status = Integer.parseInt(req.getParameter("status"));
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUsername(username);
        newExpress.setUserPhone(userPhone);
        newExpress.setStatus(status);
        boolean flag = expressService.update(id,newExpress);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(1);
            msg.setResult("修改失败");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req,HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flag = expressService.delete(id);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJSON(msg);
    }
}
